package com.doform.android.xianggucheng.activity.downorder;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.AddressBean;
import com.doform.android.xianggucheng.bean.ClientBean;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.RexUtils;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/6/5.
 * GitHub：https://github.com/cxydxpx
 */

public class UpdateAddressActivity extends BaseTitleActivity {

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_address)
    EditText et_address;

    @BindView(R.id.et_phone)
    EditText et_phone;

    private String mName;
    private String mAddress;
    private String mPhone;
    private AddressBean mAddressBean;
    private ClientBean.ListBean mClientBean;

    @Override
    protected int layoutResId() {
        return R.layout.activity_new_and_update_address;
    }

    @Override
    protected void init() {
        AddressBean addressBean = (AddressBean) getIntent().getSerializableExtra(Constans.EXTRA_UPDATE_ADDRESS);
        ClientBean.ListBean clientBean = (ClientBean.ListBean) getIntent().getSerializableExtra(Constans.EXTRA_UPDATE_ADDRESS2_ID);
        this.mAddressBean = addressBean;
        this.mClientBean = clientBean;
    }


    @Override
    protected void showView() {
        if (mAddressBean != null) {
            mName = mAddressBean.getLnkname();
            mAddress = mAddressBean.getAddr();
            mPhone = mAddressBean.getMobile();

            et_name.setText(mName);
            et_address.setText(mAddress);
            et_phone.setText(mPhone);

            et_phone.requestFocus();
            et_address.requestFocus();
            et_name.requestFocus();
        }

        titleBar.setTextTitle("设置收货地址");
    }


    @OnClick({R.id.btn_submit})
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                beforeSubmit();
                break;
            default:
                break;
        }
    }

    private void beforeSubmit() {
        String mName = et_name.getText().toString().trim();
        String mAddress = et_address.getText().toString().trim();
        String mPhone = et_phone.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            Toast.makeText(this, "请输入联系人", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(mAddress)) {
            TsUtils.showToast("请输入联系人地址");
            return;
        } else if (RexUtils.isMobileNO(mPhone) || TextUtils.isEmpty(mPhone)) {
            TsUtils.showToast("联系人电话格式不正确");
            return;
        }else if (mClientBean == null || mAddressBean == null){
            TsUtils.showToast("网络错误");
            return;
        }
        postDataToService(mName, mAddress, mPhone);
    }

    private void postDataToService(String mName, String mAddress, String mPhone) {
        HttpManager.instance()
                .updateAddress(mClientBean.getId(), mAddressBean.getGuid(), mName, mAddress, mPhone)
                .subscribe(new ErrorHandleSubscriber<AddressBean>() {
                    @Override
                    public void onNext(@NonNull AddressBean addressBean) {
                        Intent intent = new Intent(UpdateAddressActivity.this, SubmitOrderActivity.class);
                        intent.putExtra(Constans.EXTRA_SUBMIT_ORDER, addressBean);
                        intent.putExtra(Constans.EXTRA_SUBMIT_ORDER_CLIENT_ID, mClientBean.getId());
                        startActivity(intent);
                        finish();
                    }
                });
    }
}
